﻿<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ultimate Harem</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body id="default">
    <div id="header">
        <div id="banner">
            <a href="Home.aspx">
                <img src="img/UltimateHaremBanner.png" alt="Ultimate Harem Banner" /></a>
        </div>
        <ul id="nav">
            <li><a href="Home.aspx">Home</a></li>
            <li><a href="Default.aspx">Harems</a>
                <ul>
                    <li><a href="Home.aspx">JR</a></li>
                    <li><a href="Home.aspx">Ming</a></li>
                    <li><a href="Home.aspx">Daniel</a></li>
                    <li><a href="Home.aspx">Joven</a></li>
                    <li><a href="Home.aspx">Garrett</a></li>
                    <li><a href="Home.aspx">Boston</a></li>
                    <li><a href="Home.aspx">Luke</a></li>
                    <li><a href="Home.aspx">Yuria</a></li>
                    <li><a href="Home.aspx">Kathy</a></li>
                </ul>
            </li>
            <li><a href="Default.aspx">Voting Arena</a>
                <ul>
                    <li><a href="Home.aspx">Contestants</a></li>
                </ul>
            </li>
            <li><a href="Default.aspx">Drafting</a></li>
            <li><a href="Default.aspx">Competitions</a>
                <ul>
                    <li><a href="Home.aspx">Best Girl Toss-Up</a></li>
                </ul>
            </li>
        </ul>
    </div>
    <div style="clear: left;"></div>
    <div id="newsfeed">
        <!-- <img src="img/Kanade Tachibana-face.jpg" alt="Kanade Tachibana" height="150" width="150" style="float: left;"/> -->
        <img src="img/Madoka Kaname.png" alt="Madoka Kaname" height="150" width="150" style="float: right;" />
        <img src="img/Yoko Littner-face.jpg" alt="Yoko Littner" height="150" width="150" style="float: left;" />
        <div style="clear: both;"></div>
        <div id="battlebox">
            <div id="left" style="float: left;">
                <div id="nameLeft">ERROR</div>
                <div id="hpLeft">ERROR</div>
                <div id="powerLeft">ERROR</div>
                <div id="mindLeft">ERROR</div>
                <div id="animaLeft">ERROR</div>
                <div id="defenseLeft">ERROR</div>
                <div id="protectionLeft">ERROR</div>
                <div id="preservationLeft">ERROR</div>
                <button type="button" onclick="attackAction(aFighter, bFighter, 1)">Close Combat</button><br />
                <button type="button" onclick="attackAction(aFighter, bFighter, 2)">Cast Magic</button><br />
                <button type="button" onclick="attackAction(aFighter, bFighter, 3)">Spiritual Attack</button>
            </div>
            <div id="right" style="text-align: right; float: right;">
                <div id="nameRight">ERROR</div>
                <div id="hpRight">ERROR</div>
                <div id="powerRight">ERROR</div>
                <div id="mindRight">ERROR</div>
                <div id="animaRight">ERROR</div>
                <div id="defenseRight">ERROR</div>
                <div id="protectionRight">ERROR</div>
                <div id="preservationRight">ERROR</div>
            </div>
            <div id="battletext"></div>
            <div style="clear: both;"></div>
        </div>
        <div style="clear: both;"></div>

        <script src="Fighter.js"></script>
        <script>
            // Test
            var newSky = new Sky();

            console.log(newSky.getClouds());
            console.log(newSky.blue.getColor());
            newSky.blue.setColor(5);
            console.log(newSky.blue.getColor());
            

            // Fighter Information
            var kanade = {
                firstName: "Kanade",
                lastName: "Tachibana",
                hp: 108,
                power: 9,
                anima: 15,
                mind: 12,
                defense: 13,
                protection: 10,
                preservation: function () { return Math.floor((this.defense + this.protection) / 2) },
                speed: 9,
                battleAction: 0,
                isStunned: false,
                fullName: function () { return this.firstName + " " + this.lastName }
            };

            var yoko = {
                firstName: "Yoko",
                lastName: "Littner",
                hp: 98,
                power: 15,
                anima: 13,
                mind: 11,
                defense: 11,
                protection: 13,
                preservation: function () { return Math.floor((this.defense + this.protection) / 2) },
                speed: 9,
                specialty: 1,
                battleAction: 0,
                isStunned: false,
                fullName: function () { return this.firstName + " " + this.lastName }
            };

            var madoka = {
                firstName: "Madoka",
                lastName: "Kaname",
                hp: 100,
                power: 12,
                anima: 10,
                mind: 15,
                defense: 10,
                protection: 12,
                preservation: function () { return Math.floor((this.defense + this.protection) / 2) },
                speed: 9,
                specialty: 2,
                battleAction: 0,
                isStunned: false,
                fullName: function () { return this.firstName + " " + this.lastName }
            };

            // Left Shorthand Variables
            var nameLeft = document.getElementById("nameLeft");
            var hpLeft = document.getElementById("hpLeft");
            var powerLeft = document.getElementById("powerLeft");
            var animaLeft = document.getElementById("animaLeft");
            var defenseLeft = document.getElementById("defenseLeft");
            var protectionLeft = document.getElementById("protectionLeft");
            var preservationLeft = document.getElementById("preservationLeft");

            // Right Shorthand Variables
            var nameRight = document.getElementById("nameRight");
            var hpRight = document.getElementById("hpRight");
            var powerRight = document.getElementById("powerRight");
            var animaRight = document.getElementById("animaRight");
            var defenseRight = document.getElementById("defenseRight");
            var protectionRight = document.getElementById("protectionRight");
            var preservationRight = document.getElementById("preservationRight");

            // Fighter Variables
            var aFighterHP, aFighterPower, aFighterMind, aFighterAnima, aFighterDefense, aFighterProtection, aFighterPreservation, aFighterSpeed;
            var bFighterHP, bFIghterPower, bFighterMind, bFighterAnima, bFighterDefense, bFighterProtection, bFighterPreservation, bFighterSpeed;

            // Error Checking Variables
            var testMode = true;
            var loadInfo = false;

            // BattleText Shorthand Variable
            var battleText = document.getElementById("battletext");

            // Read Battle State Variable
            var battleState = 0;

            // Read Battle Action Variable
            var battleActionUser = 0;
            var battleActionAI = 0;

            // RNG Functions
            function getRandom() {
                return Math.random()
            }
            function getRandomArbitrary(min, max) {
                return Math.random() * (max - min) + min;
            }

            // Invoke the Load Information Function for Each Fighter
            var aFighter = yoko;
            var bFighter = madoka;
            writeInfo(aFighter, bFighter);

            // Write Information for Each Fighter
            function writeInfo(aFighter, bFighter) {
                if (!loadInfo) {
                    aFighterHP = aFighter.hp;
                    bFighterHP = bFighter.hp;
                    
                    nameLeft.innerHTML = aFighter.fullName();
                    if (testMode) {
                        hpLeft.innerHTML = "HP: " + aFighter.hp;
                        powerLeft.innerHTML = "Power: " + aFighter.power;
                        mindLeft.innerHTML = "Mind: " + aFighter.mind;
                        animaLeft.innerHTML = "Anima: " + aFighter.anima;
                        defenseLeft.innerHTML = "Defense: " + aFighter.defense;
                        protectionLeft.innerHTML = "Protection: " + aFighter.protection;
                        preservationLeft.innerHTML = "Preservation: " + aFighter.preservation();
                    }

                    nameRight.innerHTML = bFighter.fullName();
                    if (testMode) {
                        hpRight.innerHTML = "HP: " + bFighter.hp;
                        powerRight.innerHTML = "Power: " + bFighter.power;
                        mindRight.innerHTML = "Mind: " + bFighter.mind;
                        animaRight.innerHTML = "Anima: " + bFighter.anima;
                        defenseRight.innerHTML = "Defense: " + bFighter.defense;
                        protectionRight.innerHTML = "Protection: " + bFighter.protection;
                        preservationRight.innerHTML = "Preservation: " + bFighter.preservation();
                    }
                    verifyInfo(aFighter, bFighter)
                }
            };

            // Error Checking for Fighters
            function verifyInfo(aFighter, bFighter) {
                if (nameLeft.innerHTML == aFighter.fullName() && nameRight.innerHTML == bFighter.fullName()) {
                    loadInfo = true;
                    console.log("Console: If you can read this, you are a super 1337 H4X0R.")
                    console.log("Console: Fighter information loaded.")
                }
                else if (!loadInfo) {
                    console.log("Console: If you can read this, you are a super 1337 H4X0R.")
                    console.log("Console: [ERROR] Fighter information not loaded.");
                }
            }

            // Actions
            function attackAction(aFighter, bFighter, battleActionUser) {
                if (aFighter.hp > 0 && bFighter.hp > 0) {
                    // Run AI to decide what action for it to take
                    battleActionAI = runAI(bFighter, battleActionAI);

                    // Speed Calculations
                    aFighterSpeed = aFighter.speed;
                    bFighterSpeed = bFighter.speed;
                    speedCalculate(aFighter, bFighter, aFighterSpeed, bFighterSpeed, battleActionUser, battleActionAI);

                    // Turn Order
                    if (aFighterSpeed > bFighterSpeed) {
                        if (battleState < 3) {
                            battleState = 1;
                            damageCalculation(aFighter, bFighter, battleActionUser);
                            writeDamage(aFighter, bFighter);
                            checkFighters(aFighter, bFighter);
                        }
                        if (battleState < 3) {
                            battleState = 2;
                            damageCalculation(aFighter, bFighter, battleActionAI);
                            writeDamage(aFighter, bFighter);
                            checkFighters(aFighter, bFighter);
                        }
                    }
                    if (bFighterSpeed > aFighterSpeed) {
                        if (battleState < 3) {
                            battleState = 2;
                            damageCalculation(aFighter, bFighter, battleActionAI);
                            writeDamage(aFighter, bFighter);
                            checkFighters(aFighter, bFighter);
                        }
                        if (battleState < 3) {
                            battleState = 1;
                            damageCalculation(aFighter, bFighter, battleActionUser);
                            writeDamage(aFighter, bFighter);
                            checkFighters(aFighter, bFighter);
                        }
                    }
                    if (aFighterSpeed == bFighterSpeed) {
                        if (battleState < 3) {
                            battleState = 1;
                            damageCalculation(aFighter, bFighter, battleActionUser);
                            writeDamage(aFighter, bFighter);

                            battleState = 2;
                            damageCalculation(aFighter, bFighter, battleActionAI);
                            writeDamage(aFighter, bFighter);
                            checkFighters(aFighter, bFighter);
                        }
                    } // Matt has a tiny butthole now that JR's tiny penis has shrank it. (:
                }
            };

                // Run AI
            function runAI(bFighter, battleActionAI) {
                var combatChance;
                var magicChance;
                var spiritChance;
                var rngChance = (getRandom() * 10);

                if (bFighter.specialty == 1) {
                    combatChance = 9;
                    magicChance = 9.5;
                    spiritChance = 10;
                    if (rngChance <= combatChance) {
                        return 1;
                    }
                    if (rngChance > combatChance && rngChance <= magicChance) {
                        return 2;
                    }
                    if (rngChance > magicChance && rngChance <= spiritChance) {
                        return 3;
                    }
                }
                if (bFighter.specialty == 2) {
                    combatChance = 9.5;
                    magicChance = 9;
                    spiritChance = 10;
                    if (rngChance <= magicChance) {
                        return 2;
                    }
                    if (rngChance > magicChance && rngChance <= combatChance) {
                        return 1;
                    }
                    if (rngChance > combatChance && rngChance <= spiritChance) {
                        return 3;
                    }
                }
                if (bFighter.specialty == 3) {
                    combatChance = 10;
                    magicChance = 9.5;
                    spiritChance = 9;
                    if (rngChance <= spiritChance) {
                        return 3;
                    }
                    if (rngChance > spiritChance && rngChance <= magicChance) {
                        return 2;
                    }
                    if (rngChance > magicChance && rngChance <= combatChance) {
                        return 1;
                    }
                }
            }

            // Compare Speed
            function speedCalculate(aFighter, bFighter, aFighterSpeed, bFighterSpeed, battleAction, battleActionAI) {
                var actionSpeed = 0;
                if (battleAction == 1) {
                    actionSpeed = 1;
                    aFighterSpeed = aFighter.speed * actionSpeed;
                }
                if (battleAction == 2) {
                    actionSpeed = 0.5;
                    aFighterSpeed = aFighter.speed * actionSpeed;
                }
                if (battleAction == 3) {
                    actionSpeed = 2;
                    aFighterSpeed = aFighter.speed * actionSpeed;
                }
                if (battleActionAI == 1) {
                    actionSpeed = 1;
                    bFighterSpeed = bFighter.speed * actionSpeed;
                }
                if (battleActionAI == 2) {
                    actionSpeed = 0.5;
                    bFighterSpeed = bFighter.speed * actionSpeed;
                }
                if (battleActionAI == 3) {
                    actionSpeed = 2;
                    bFighterSpeed = bFighter.speed * actionSpeed;
                }
            };

            // Damage Calculation
            function damageCalculation(aFighter, bFighter, battleAction) {

                // User
                if (battleState == 1) {
                    if (!aFighter.isStunned) {
                        if (battleAction == 1) {
                            physicalCalculation(aFighter, bFighter);
                        }
                        if (battleAction == 2) {
                            magicCalculation(aFighter, bFighter);
                        }
                        if (battleAction == 3) {
                            spiritCalculation(aFighter, bFighter);
                        }
                    }
                }
                // AI
                else if (battleState == 2) {
                    if (!bFighter.isStunned) {
                        if (battleAction == 1) {
                            physicalCalculation(aFighter, bFighter);
                        }
                        if (battleAction == 2) {
                            magicCalculation(aFighter, bFighter);
                        }
                        if (battleAction == 3) {
                            spiritCalculation(aFighter, bFighter);
                        }
                    }
                }
            };

                // Write the Applied Damage to the Screen
            function writeDamage(aFighter, bFighter) {
                if (battleState == 1) {
                    hpRight.innerHTML = "HP: " + bFighterHP;
                }
                else if (battleState == 2) {
                    hpLeft.innerHTML = "HP: " + aFighterHP;
                }
                // Set HP to 0 if Less Than 0
                else if (battleState == 3) {
                    hpRight.innerHTML = "HP: " + bFighterHP; //Dicks are tasty in JR's butthole
                }
                else if (battleState == 4) {
                    hpLeft.innerHTML = "HP: " + aFighterHP;
                }
                else if (battleState == 5) {
                    hpLeft.innerHTML = "HP : " + aFighterHP;
                    hpRight.innerHTML = "HP : " + bFighterHP;
                }
            };

            // Physical Calculation
            function physicalCalculation(aFighter, bFighter) {
                aFighterPower = aFighter.power;
                bFighterPower = bFighter.power;
                aFighterDefense = aFighter.defense;
                bFighterDefense = bFighter.defense;
                var rngCrit = (getRandom() * 100);
                var critStrikePercentChance = 8;

                if (battleState == 1) {
                    aFighterPower = Math.floor(aFighterPower * getRandomArbitrary(0.7, 1) - bFighterDefense * 0.08);
                    
                    if (rngCrit <= critStrikePercentChance) {
                        bFighterHP = bFighterHP - Math.floor((aFighterPower * 1.75));
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " attacks with " + Math.floor(aFighterPower * 1.75) + " damage! <br /> A critical strike! <br />";
                    }
                    else {
                        bFighterHP = bFighterHP - aFighterPower;
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " attacks with " + aFighterPower + " damage! <br />";
                    }
                }
                else if (battleState == 2) {
                    bFighterPower = Math.floor(bFighterPower * getRandomArbitrary(0.7, 1) - aFighterDefense * 0.08);
                    
                    if (rngCrit <= critStrikePercentChance) {
                        aFighterHP = aFighterHP - Math.floor(bFighterPower * 1.75);
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " strikes back with " + Math.floor(bFighterPower * 1.75) + " damage! <br /> A critical strike! <br />";
                    }
                    else {
                        aFighterHP = aFighterHP - bFighterPower;
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " strikes back with " + bFighterPower + " damage! <br />";
                    }
                }
            };

            // Magic Calculation
            function magicCalculation(aFighter, bFighter) {
                aFighterMind = aFighter.mind;
                bFighterMind = bFighter.mind;
                aFighterProtection = aFighter.protection;
                bFighterProtection = bFighter.protection;
                var rngCrit = (getRandom() * 100);
                var critStrikePercentChance = 6;

                if (battleState == 1) {
                    aFighterMind = Math.floor((aFighterMind * getRandomArbitrary(1.05, 1.3) - bFighterProtection * 0.1));

                    if (rngCrit <= critStrikePercentChance) {
                        bFighterHP = bFighterHP - Math.floor(aFighterMind * 1.3);
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " deals " + Math.floor(aFighterMind * 1.3) + " magic damage! <br /> Hit the weak point! <br />";

                    }
                    else {
                        bFighterHP = bFighterHP - aFighterMind;
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " deals " + aFighterMind + " magic damage! <br />";
                    }
                }
                else if (battleState == 2) {
                    bFighterMind = Math.floor((bFighterMind * getRandomArbitrary(1.05, 1.3) - aFighterProtection * 0.1));

                    if (rngCrit <= critStrikePercentChance) {
                        aFighterHP = aFighterHP - Math.floor(bFighterMind * 1.3);
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " retaliates with " + Math.floor(bFighterMind * 1.3) + " magic damage! <br /> Hit the weak point! <br />";
                    }
                    else {
                        aFighterHP = aFighterHP - bFighterMind;
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " retaliates with " + bFighterMind + " magic damage! <br />";
                    }
                }
            };

            // Spirit Calculation
            function spiritCalculation(aFighter, bFighter) {
                aFighterAnima = aFighter.anima;
                bFighterAnima = bFighter.anima;
                aFighterPreservation = aFighter.preservation();
                bFighterPreservation = bFighter.preservation();

                var rngTwice = (getRandom() * 100);
                var twicePercentChance = 11;

                if (battleState == 1) {
                    aFighterAnima = Math.floor((aFighterAnima * getRandomArbitrary(0.85, 0.95) - bFighterPreservation * 0.1));

                    if (rngTwice <= twicePercentChance) {
                        aFighterHP = aFighterHP - 1;
                        bFighterHP = bFighterHP - aFighterAnima;
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " hits with " + aFighterAnima + " spirit damage! <br />";
                        bFighterHP = bFighterHP - aFighterAnima;

                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " hits twice with " + aFighterAnima + " (" + aFighterAnima * 2 + ")" + " spirit damage! <br />";
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " drains away 1 life energy. <br />";
                    }
                    else {
                        aFighterHP = aFighterHP - 1;
                        bFighterHP = bFighterHP - aFighterAnima;
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " hits with " + aFighterAnima + " spirit damage! <br />";
                        battleText.innerHTML = battleText.innerHTML + aFighter.firstName + " drains away 1 life energy. <br />";
                    }
                }
                else if (battleState == 2) {
                    bFighterAnima = Math.floor((bFighterAnima * getRandomArbitrary(0.85, 0.95) - aFighterPreservation * 0.1));

                    if (rngTwice <= twicePercentChance) {
                        bFighterHP = bFighterHP - 1;
                        aFighterHP = aFighterHP - bFighterAnima;
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " returns with " + bFighterAnima + " spirit damage! <br />";
                        aFighterHP = aFighterHP - bFighterAnima;
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " and again with " + bFighterAnima + " (" + bFighterAnima * 2 + ")" + " spirit damage! <br />";
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " drains away 1 life energy. <br />";
                    }
                    else {
                        bFighterHP = bFighterHP - 1;
                        aFighterHP = aFighterHP - bFighterAnima;
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " returns with " + bFighterAnima + " spirit damage! <br />";
                        battleText.innerHTML = battleText.innerHTML + bFighter.firstName + " drains away 1 life energy. <br />";
                    }
                }
            };

            // Check Fighters
            function checkFighters(aFighter, bFighter) {
                // Check If Fighters Reach 0 HP
                if (aFighterHP <= 0 && bFighterHP > 0 && battleState != 0) {
                    aFighterHP = 0;
                    battleState = 4;
                    writeDamage(aFighter, bFighter);
                    battleText.innerHTML = battleText.innerHTML + "Oh no! You lost! <br /> Gained 50 XP <br />";
                }
                else if (aFighterHP > 0 && bFighterHP <= 0 && battleState != 0) {
                    bFighterHP = 0;
                    battleState = 3;
                    writeDamage(aFighter, bFighter);
                    battleText.innerHTML = battleText.innerHTML + "You won! <br /> Gained 100 XP<br />";
                }
                else if (aFighterHP <= 0 && bFighterHP <= 0 && battleState != 0) {
                    aFighterHP = 0;
                    bFighterHP = 0;
                    battleState = 5;
                    writeDamage(aFighter, bFighter);
                    battleText.innerHTML = battleText.innerHTML + "You tied! <br /> Gained 75 XP<br />";
                }
            };

                // Auto Scroll
                /* window.onload = function () {
                    scrollDiv_init();
                };
                ScrollRate = 10;
    
                function scrollDiv_init() {
                    ReachedMaxScroll = false;
                    battleText.scrollTop = 0;
                    PreviousScrollTop = 0;
                    ScrollInterval = setInterval(scrollDiv(battleText), ScrollRate);
                };
    
                function scrollDiv(battleText) {
                    if (!ReachedMaxScroll) {
                        battleText.scrollTop = PreviousScrollTop;
                        PreviousScrollTop++;
                        ReachedMaxScroll = battleText.scrollTop >= (battleText.scrollHeight - battleText.offsetHeight);
                    }
                }; */

        </script>
    </div>
</body>
</html>
